﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.Client.Platform;
using Ebada.UI.Base;
namespace Ebada.Modules.Vehicle
{

    public partial class UCVehicleRepair : DevExpress.XtraEditors.XtraUserControl {
        private GridViewOperation<VehicleRepair> gridViewOperation;

        public event SendDataEventHandler<VehicleRepair> FocusedRowChanged;
        public static bool FirstLoad = false;
        public UCVehicleRepair() 
        {
            InitializeComponent();
            gridViewOperation = new GridViewOperation<VehicleRepair>(gridControl1, gridView1, barManager1,new frmVehicleRepair_sedEdit());
            gridViewOperation.CreatingObjectEvent +=gridViewOperation_CreatingObjectEvent;
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridViewOperation_FocusedRowChanged);
            gridViewOperation.BeforeAdd +=new ObjectOperationEventHandler<VehicleRepair>(gridViewOperation_BeforeAdd);
            initColumns();
            InitData();
        }

        void gridViewOperation_BeforeAdd(object render, ObjectOperationEventArgs<VehicleRepair> e)
        {
            e.Value.RepairDate = DateTime.Now;
            e.Value.FinishDate = DateTime.Now;
            e.Value.OutDate = DateTime.Now;
            
        }

        void gridViewOperation_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) 
        {
            if (FocusedRowChanged != null)
                FocusedRowChanged(gridView1, gridView1.GetFocusedRow() as VehicleRepair);
        }
        


       
        /// <summary>
        /// 设置隐藏列
        /// </summary>
        /// 
       public  void initColumns() 
        {
             for (int i = 0; i < gridView1.Columns.Count; i++)
            {
                gridView1.Columns[i].VisibleIndex = -1;
            }
            int m = 0;
            gridView1.Columns["RepairNum"].Caption = "送修单号";
            gridView1.Columns["RepairNum"].VisibleIndex = m++;
            gridView1.Columns["RepairDate"].Caption = "送修日期";
            gridView1.Columns["RepairDate"].VisibleIndex = m++;
            gridView1.Columns["VehicleNumber"].Caption = "车牌号码";
            gridView1.Columns["VehicleNumber"].VisibleIndex = m++;
            gridView1.Columns["VehicleType"].Caption = "车辆类型";
            gridView1.Columns["VehicleType"].VisibleIndex = m++;
            gridView1.Columns["VehicleOrg"].Caption = "车属单位";
            gridView1.Columns["VehicleOrg"].VisibleIndex = m++;
            gridView1.Columns["RepairUser"].Caption = "送修人";
            gridView1.Columns["RepairUser"].VisibleIndex = m++;
            gridView1.Columns["Description"].Caption = "送修原因";
            gridView1.Columns["Description"].VisibleIndex = m++;
            gridView1.Columns["CheckUser"].Caption = "批准人";
            gridView1.Columns["CheckUser"].VisibleIndex = m++;

            gridView1.Columns["OrgName"].Caption = "维修单位";
            gridView1.Columns["OrgName"].VisibleIndex = m++;
            gridView1.Columns["Tel"].Caption = "联系电话";
            gridView1.Columns["Tel"].VisibleIndex = m++;
            gridView1.Columns["RepairType"].Caption = "维修类型";
            gridView1.Columns["RepairType"].VisibleIndex = m++;
            gridView1.Columns["Price"].Caption = "预计维修费用";
            gridView1.Columns["Price"].VisibleIndex = m++;
            gridView1.Columns["FinishDate"].Caption = "预计完工日期";
            gridView1.Columns["FinishDate"].VisibleIndex = m++;
            gridView1.Columns["OutDate"].Caption = "出厂日期";
            gridView1.Columns["OutDate"].VisibleIndex = m++;
            gridView1.Columns["Comments"].Caption = "批准意见";
            gridView1.Columns["Comments"].VisibleIndex = m++;
           
          
        }
      
    
        /// <summary>
        /// 封装了数据操作的对象
        /// </summary>
        [Browsable(false)]
       public GridViewOperation<VehicleRepair> GridViewOperation
       {
            get { return gridViewOperation; }
            set { gridViewOperation = value; }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(VehicleRepair newobj)
        {
            //newobj.VehiclesRepair_ID = Guid.NewGuid().ToString();
           
        }

        public void InitData()
        {
            gridViewOperation.RefreshData(" where FinishDate2= '1900-1-1' ");
        }
        //修完登记
       
        private void btEdit2_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
            {
                MessageBox.Show("无维修信息，不能进行完工登记！");
                return;
            }
            VehicleRepair tempvr = (Ebada.Platform.Model.VehicleRepair)(gridView1.GetFocusedRow());
            frmVehicleRepair_bakEdit frm = new frmVehicleRepair_bakEdit();
            tempvr.FinishDate2 = DateTime.Now;
            frm.RowData = tempvr;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                 MainHelper.PlatformSqlMap.Update<VehicleRepair>((VehicleRepair)frm.RowData);
                InitData();
            }
        }
      
       

    }
}
